﻿using Kingdee.BOS.Core.Report.PlugIn;
using Kingdee.BOS.Core.Report.PlugIn.Args;
using Kingdee.BOS.Core.Util;
using Kingdee.BOS.Util;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DzInfo.FormPlugIn.ND
{
    [Description("ND：应收账龄分析表格式化金额"), HotUpdate]
    public class NewAgingAnalysis : AbstractSysReportPlugIn
    {
        public override void FormatCellValue(FormatCellValueArgs args)
        {
            base.FormatCellValue(args);
            if (args.Header.Key.Equals("FTotalAmount", StringComparison.OrdinalIgnoreCase))
            {
                // 格式化数字（指定小数位数+千分位符）

                args.FormateValue = GetDecimalFormatString(Convert.ToDecimal(args.DataRow["FTotalAmount"]), 2);

                // 去尾零

                args.FormateValue = args.FormateValue.GetNoZeroString();
            }
            if (args.Header.Key.Equals("FTotalAmountBW", StringComparison.OrdinalIgnoreCase))
            {
                // 格式化数字（指定小数位数+千分位符）

                args.FormateValue = GetDecimalFormatString(Convert.ToDecimal(args.DataRow["FTotalAmountBW"]), 2);

                // 去尾零

                args.FormateValue = args.FormateValue.GetNoZeroString();
            }
        }

        private string GetDecimalFormatString(decimal value, int digits)
        {
            if (digits < 0 || digits > 99) return ObjectUtils.Object2String(value);

            var formatString = "N" + digits;

            var format = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();

            format.NumberNegativePattern = 1;

            format.NumberDecimalDigits = digits;

            return value.ToString(formatString, format);

        }
    }
}
